<?php
/**
 * 獲取該當前用戶的所在組ID或名稱
 * @param  [int] $uid  // 用戶ID 
 * @param  string $name  // 默認輸出所在組名稱, ID為輸出所在組ID
 * @return [type]
 */
function get_user_group($uid,$name ='title'){
		
		$info = D('UsersView')->getOne(array('uid' => $uid));
		if(tep_not_null($info['group_id'])){
			$group = M('think_auth_group')->where(array('id'=> $info['group_id']))->find();
			if($name == 'title')
				return $group['title'];  
			else
				return $group['id'];  
		}else{
			return 	'NUll';
		}
	}


/**
 * 刪除產品圖片
 * @param  [int] $pid  // 產品ID
 * @return [type]  null
 */
function remove_products_image($pid){
		$mainImg = M('products')->where(array('products_id' => $pid))->getField('products_image');
		$BigImg = M('products_images')->where(array('products_id' => $pid))->select();
		foreach($BigImg as $v){
			@unlink("./Public/images/products/".$v['image']);
			@unlink("./Public/images/products/thumb/s_".$v['image']);
			@unlink("./Public/images/products/thumb/m_".$v['image']);
		}
		@unlink("./Public/images/products/".$mainImg);
		@unlink("./Public/images/products/thumb/s_".$mainImg);
		@unlink("./Public/images/products/thumb/m_".$mainImg);
		
	}
/**
 * 刪除文件夾和該文件夾的的所有文件
 * @param  [string]  $path
 * @param  boolean $delDir
 * @return [boolean]
 */
function delDirAndFile($path, $delDir = FALSE) {
    $handle = opendir($path);
    if ($handle) {
        while (false !== ( $item = readdir($handle) )) {
            if ($item != "." && $item != "..")
                is_dir("$path/$item") ? delDirAndFile("$path/$item", $delDir) : unlink("$path/$item");
        }
        closedir($handle);
        if ($delDir)
            return rmdir($path);
    }else {
        if (file_exists($path)) {
            return unlink($path);
        } else {
            return FALSE;
        }
    }
}
/**
 * 獲取當前URL的所有參數
 * @param  string $exclude_array
 * @return [string]
 */
 function get_all_get_params($exclude_array = '') {

    if (!is_array($exclude_array)) $exclude_array = array();
	
    $get_url = '';
    if (is_array($_GET) && (sizeof($_GET) > 0)) {
      reset($_GET);
      while (list($key, $value) = each($_GET)) {
        if ( is_string($value) && (strlen($value) > 0) && (!in_array($key, $exclude_array)) && ($key != 'x') && ($key != 'y') ) {
          $get_url .= $key . '=' . rawurlencode(stripslashes($value)) . '&';
        }
      }
    }

    return $get_url;
  }
/**
 * 根據不同的字段,查詢sql快速排序按鈕
 * @param  [string] $field
 * @return [string]
 */
function createUpDownLink($field,$index = 'index')
  {
    
    $upIcon = '__PUBLIC__/images/icons/16x16/up_16.png';
    $downIcon = '__PUBLIC__/images/icons/16x16/down_16.png';
    if((isset($_GET['sort'] ) && !empty($_GET['sort'])) && (isset($_GET['updown'] ) && !empty($_GET['updown'])))
    {
      if($field === $_GET['sort'])
      {
        if($_GET['updown'] === 'asc')
          $upIcon = '__PUBLIC__/images/icons/16x16/up_red_16.png';
        else if ($_GET['updown'] === 'desc')
          $downIcon ='__PUBLIC__/images/icons/16x16/down_red_16.png';
        
      }
    }
    $upLink = U($index,'sort='.$field.'&updown=asc&'.get_all_get_params(array('sort','updown')));
    $downLink = U($index, 'sort='.$field.'&updown=desc&'.get_all_get_params(array('sort','updown')));
    $upDownLink = '<span style="margin-left: 10px; vertical-align: top"><a href="'.$upLink.'"><img src="' . $upIcon . '" ></a>&nbsp;<a href="'.$downLink.'"><img src="' . $downIcon . '"/></a></span>';
    
    return $upDownLink;
  }
  

?>